package sort;

/**
 * 荷兰国旗
 * Created by JiaJia on 2019/8/12 0012.
 */
public class SortColors {
    public static void main(String[] args) {
        int[] nums=new int[]{2,0,2,1,1,0};
        //System.out.println(nums);
        int[] newNum=sortColors(nums);
        for (int i = 0; i <nums.length ; i++) {
            System.out.print(newNum[i]+" ");
        }
        //System.out.println(sortColors(nums));
    }
    private static int[] sortColors(int[] nums){
        int less=-1,pos=0,more=nums.length;
        while (pos<more){
            if (nums[pos]==0){
                swap(nums,++less,pos++);
            }else if (nums[pos]==2){
                swap(nums,--more,pos);
            }else{
                ++pos;
            }

        }
        return nums;
    }
    private static void swap(int[] nums,int i,int j){
        int tmp=nums[i];
        nums[i]=nums[j];
        nums[j]=tmp;
    }
}